﻿using MySql.Data.MySqlClient;

using NewChillers.Helpers;
using NewChillers.Models;
using NewChillers.Utils;
using NewChillers.Views;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace NewChillers.ViewModels
{
   
    public class SelectRecipeViewModel: ObservableObject
    {
        #region 事件
        // 在属性区域添加事件定义
        public event EventHandler<RecipeSelectedEventArgs> RecipeSelectedEvent;

        public class RecipeSelectedEventArgs : EventArgs
        {
            public RecipeModel SelectedRecipe { get; set; }

            public RecipeSelectedEventArgs(RecipeModel recipe)
            {
                SelectedRecipe = recipe;
            }
        }
        #endregion

        #region 属性

        private SelectRecipeView page;

        private RecipeModel _recipeSelected;
        public RecipeModel RecipeSelected
        {
            get { return _recipeSelected; }
            set
            {
                _recipeSelected = value;
                RaisePropertyChanged();
            }
        }
        public ObservableCollection<RecipeModel> RecipeList { get; set; } = new ObservableCollection<RecipeModel>();

        //方法
        public ICommand DoubleClickCommand { get; set;}

        #endregion
        //构造函数
        public SelectRecipeViewModel(SelectRecipeView page,string recipeProcess)
        { 
            //加载数据
            LoadData(recipeProcess);
            this.page = page;
            DoubleClickCommand = new RelayCommand(DoubleClick);
        }

        /// <summary>
        /// 双击事件
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void DoubleClick(object obj)
        {
            //赋值给主页面
            if (_recipeSelected != null)
            {
                RecipeSelectedEvent?.Invoke(this, new RecipeSelectedEventArgs(_recipeSelected));
            }
            //关闭页面
            page.Close();
        }

        /// <summary>
        /// 加载配方数据
        /// </summary>
        private void LoadData(string repiceProcessString)
        {
            int repiceProcess = Convert.ToInt32(repiceProcessString);
            //加载数据
            string selectSql = @"SELECT * FROM recipe_info WHERE recipe_process = @recipeProcess";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@recipeProcess", repiceProcess));
            RecipeList.Clear();

            foreach (var item in MysqlHelper.ExecuteQuery<RecipeModel>(selectSql,
                (reader) => {
                    return new RecipeModel
                    {
                        RecipeId = Convert.ToInt32(reader["recipe_id"]),
                        RecipeName = reader["recipe_name"].ToString(),
                        RecipeProcess = Convert.ToInt32(reader["recipe_process"]),
                        CycleCount = Convert.ToInt32(reader["cycle_count"]),
                        DrawValue = reader["draw_value"].ToString(),
                        DrawKeepTime = Convert.ToInt32(reader["draw_keep_time"]),
                        PumpValue = reader["pump_value"].ToString(),
                        PumpKeepTime = Convert.ToInt32(reader["pump_keep_time"]),
                        VentValue = reader["vent_value"].ToString(),
                        StablePressureValue = reader["stable_pressure_value"].ToString(),
                        StablePressureKeepTime = Convert.ToInt32(reader["stable_pressure_keep_time"]),
                        CreateTime = Convert.ToDateTime(reader["create_time"]),
                        UpdateTime = Convert.ToDateTime(reader["update_time"]),
                        RecipeRemark = reader["recipe_remark"].ToString()
                    };
                },
                parameters.ToArray()))
            {
                RecipeList.Add(item);
            }
        }
    }
}
